package transforms;

import java.util.Iterator;

import org.objectweb.asm.tree.FieldNode;

import com.rshacking.rhf.files.ClassContainer;
import com.rshacking.rhf.updater.Transform;
import com.rshacking.rhf.updater.hooks.ClassHook;
import com.rshacking.rhf.updater.hooks.HooksList;

public class FileOnDiskTransform extends Transform {

        @Override
        public boolean canActivate(final HooksList hooks, final ClassContainer cc) {
                return cc.containsFieldType("Ljava/io/RandomAccessFile;");
        }

        @Override
        public boolean execute(final HooksList hooks, final ClassContainer cc) {
                final ClassHook ch = hooks.add("FileOnDisk", cc.getName());
                final Iterator<FieldNode> it = cc.getASM().fields.iterator();
                while (it.hasNext()) {
                        final FieldNode fn = it.next();
                        if (fn.desc.equals("Ljava/io/RandomAccessFile;")) {
                            ch.add("randomAccessFile", fn);
                        } else if (fn.desc.equals("Ljava/io/File;")) {
                            ch.add("file", fn);
                        }
                }
                return true;
        }

}